import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// 车辆类（保持不变）
class Vehicle {
    public enum State { STOPPED, ACCELERATING, CRUISING}

    private final int id;
    private final int laneId;
    private int position;
    private int speed;
    private State state;
    private final Color color;
    private static final Random random = new Random();

    private int x;
    private int y;

    private Vehicle pre;

    public boolean count_flag=false;

    public Vehicle(int id, int laneId,int position,Vehicle pre) {
        this.pre=pre;
        this.id = id;
        this.laneId = laneId;
        this.position = position-80;
        this.speed = 0;
        this.state = State.CRUISING;
        this.color = new Color(
                100 + random.nextInt(156),
                100 + random.nextInt(156),
                100 + random.nextInt(156)
        );
    }

    public void update(EdgeDevice.LightState lightState) {
        if(laneId==1){
            if(lightState== EdgeDevice.LightState.YELLOW||lightState==EdgeDevice.LightState.RED) {
                if (y >= 185 && y <= 300) {
                    this.state = State.ACCELERATING;
                }
                else if(y<185 && y>155){
                    this.state = State.STOPPED;
                }
            }else if(y>300){
                this.state = State.ACCELERATING;
            }
            else {
                this.state = State.CRUISING;
            }
        }else if(laneId==2){
            if(lightState== EdgeDevice.LightState.YELLOW||lightState==EdgeDevice.LightState.RED) {
                if (x >= 135 && x <= 250) {
                    this.state = State.ACCELERATING;
                }
                if (x < 135 && x >= 105) {
                    this.state = State.STOPPED;
                }
            }else if(x>250){
                this.state = State.ACCELERATING;
            }
            else {
                this.state = State.CRUISING;
            }
        }else if(laneId==3){
            if(lightState== EdgeDevice.LightState.YELLOW||lightState==EdgeDevice.LightState.RED) {
                if (y >= 170 && y <= 285) {
                    this.state = State.ACCELERATING;
                }
                else if(y>290 && y<=330){
                    this.state = State.STOPPED;
                }
            }else if(y<170){
                this.state = State.ACCELERATING;
            }
            else {
                this.state = State.CRUISING;
            }
        }else if(laneId==4){
            if(lightState== EdgeDevice.LightState.YELLOW||lightState==EdgeDevice.LightState.RED) {
                if (x >= 120 && x <= 235) {
                    this.state = State.ACCELERATING;
                }
                if (x <= 270 && x > 235) {
                    this.state = State.STOPPED;
                }
            }else if(x<120){
                this.state = State.ACCELERATING;
            }
            else {
                this.state = State.CRUISING;
            }
        }else{
            throw new IllegalArgumentException("laneId不存在！");
        }

        if(this.state==State.ACCELERATING){
            speed=35;
        }else if(this.state==State.CRUISING){
            speed=18;
        }else if(this.state==State.STOPPED){
            speed=0;
        }
        position += speed;
    }
    public int[] draw(Graphics g, int baseX, int baseY, EdgeDevice.LightState lightState) {
        int old_x = x;int old_y = y;
        if(pre!=null&&laneId==1&&(pre.y>=650||this.y>=600)){
            pre=null;
        }else if(pre!=null&&laneId==2&&(pre.x>=750||this.x>=700)){
            pre=null;
        }else if(pre!=null&&laneId==3&&(pre.y<=-30||this.y<=-30)){
            pre=null;
        }else if(pre!=null&&laneId==4&&(pre.x<=-30||this.x<=-30)){
            pre=null;
        }
        if(laneId==1){
            x=baseX-25;y=baseY+position-20;
        }else if(laneId==2){
            x=baseX+position;y=baseY+10;
        }else if(laneId==3){
            x=baseX+15;y=baseY-position;
        }else if(laneId==4){
            x=baseX-position;y=baseY-25;
        }else{
            throw new IllegalArgumentException("laneId不存在");
        }


        // 与前车的距离不能超过10
        if (pre != null && laneId == 1 && pre.y - y < 40) {
            this.y = pre.y - 45;
        } else if (pre != null && laneId == 2 && pre.x - x < 40) {
            this.x = pre.x - 45;
        } else if (pre != null && laneId == 3 && y - pre.y < 40) {
            this.y = pre.y + 45;
        } else if (pre != null && laneId == 4 && x - pre.x < 40) {
            this.x = pre.x + 45;
        }

        if(this.state==State.STOPPED){
            y=old_y;x=old_x;
        }

        if((this.state!=State.STOPPED)&&(pre!=null)&&pre.state==State.STOPPED){
            if((laneId==1&&this.y==pre.y-45)||(laneId==2&&this.x==pre.x-45)||(laneId==3&&this.y==pre.y+45)||(laneId==4&&this.x==pre.x+45)) {
                state = State.STOPPED;
            }
        }
        if(laneId==1){
            g.setColor(color);
            g.fillRect(x, y, 18, 30);
            g.setColor(new Color(200, 200, 255, 200));
            g.fillRect(x + 4, y + 5, 6, 20);
            g.setColor(state == State.STOPPED ? Color.RED : Color.YELLOW);
            g.fillRect(x + 6, y + 3, 4, 4);
        }
        else if(laneId==2) {
            g.setColor(color);
            g.fillRect(x, y, 30, 18);
            g.setColor(new Color(200, 200, 255, 200));
            g.fillRect(x + 5, y + 4, 20, 6);
            g.setColor(state == State.STOPPED ? Color.RED : Color.YELLOW);
            g.fillRect(x +  3, y + 6, 4, 4);
        }else if(laneId==3){
            g.setColor(color);
            g.fillRect(x, y, 18, 30);
            g.setColor(new Color(200, 200, 255, 200));
            g.fillRect(x + 4, y + 5, 6, 20);
            g.setColor(state == State.STOPPED ? Color.RED : Color.YELLOW);
            g.fillRect(x + 6, y + 23, 4, 4);
        }else if(laneId==4){
            g.setColor(color);
            g.fillRect(x, y, 30, 18);
            g.setColor(new Color(200, 200, 255, 200));
            g.fillRect(x + 5, y + 4, 20, 6);
            g.setColor(state == State.STOPPED ? Color.RED : Color.YELLOW);
            g.fillRect(x +  23, y + 6, 4, 4);
        }else{
            throw new IllegalArgumentException("laneId不存在！");
        }

        //前进距离
        if(laneId==1){
            return new int[]{1,y};
        }else if(laneId==2){
            return new int[]{2,x};
        }else if(laneId==3){
            return new int[]{3,y};
        }else if(laneId==4){
            return new int[]{4,x};
        }else {
            throw new IllegalArgumentException("laneId不存在!");
        }
    }

    public boolean hasLeftIntersection() {
        if(laneId==1){
            return y>=650;
        }else if(laneId==2){
            return x>=750;
        }else if(laneId==3){
            return y<=-30;
        }else if(laneId==4){
            return x<=-30;
        }else{
            throw new IllegalArgumentException("laneId不存在！");
        }
    }

    public void setPreVehicle(Vehicle pre){
        this.pre=pre;
    }

    public Vehicle getPreVehicle(){
        return this.pre;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public int getId(){
        return id;
    }

    public int getLaneId() { return laneId; }
    public State getState() { return state; }
    public int getPosition(){return position;}
}
